package com.ruoyi.buss.grid.sq.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.buss.grid.sh.domain.DataGtgsh;
import com.ruoyi.buss.grid.sq.domain.DataWgWgsqSq;
import com.ruoyi.buss.grid.sq.domain.DataWgWgsqSqgl;
import com.ruoyi.buss.grid.sq.service.IDataWgWgsqSqService;
import com.ruoyi.buss.grid.sq.service.IDataWgWgsqSqglService;
import com.ruoyi.buss.utils.ListUtil;
import com.ruoyi.common.core.enums.ErrorCode;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.buss.grid.sq.domain.DataWgWgsq;
import com.ruoyi.buss.grid.sq.service.IDataWgWgsqService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 网格社区Controller
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Api(tags = {"网格社区用户controller"})
@RestController
@RequestMapping("/gridSq")
public class DataWgWgsqController extends BaseController
{
    @Autowired
    private IDataWgWgsqService dataWgWgsqService;

    @Autowired
    private IDataWgWgsqSqService dataWgWgsqSqService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询网格社区列表
     */
    @ApiOperation(value="查询网格社区列表",notes="注意问题点")
    @PreAuthorize(hasPermi = "buss:gridSq:queryGridSqAll")
    @GetMapping("/queryGridSqAll")
    public AjaxResult queryGridSqAll(DataWgWgsq dataWgWgsq)
    {
        Map<String,Object> map = new HashMap<>();
        LoginUser userInfo = tokenService.getLoginUser();

        /**
         * 查询所有概况汇总信息
         */
        DataWgWgsqSq dataWgWgsqSq = new DataWgWgsqSq();
        dataWgWgsqSq.setSssq(dataWgWgsq.getSssq());

        if(!userInfo.getRoles().contains("zhhz")
                && !userInfo.getRoles().contains("common")){
            dataWgWgsqSq = dataWgWgsqSqService.queryAllSqHz(dataWgWgsqSq,userInfo);
        } else {
            dataWgWgsqSq = dataWgWgsqSqService.selectDataWgWgsqSqByUserInfo(dataWgWgsqSq,userInfo);
        }
        map.put("dataWgWgsqSq",dataWgWgsqSq);
        dataWgWgsqSq = new DataWgWgsqSq();
        if(userInfo.getRoles().contains("zhhz")){
            dataWgWgsq.setOrgId(userInfo.getSysUser().getDept().getOrgId());
            dataWgWgsqSq.setOrgId(userInfo.getSysUser().getDept().getOrgId());
        } else if(userInfo.getRoles().contains("common")){
            dataWgWgsq.setOrgId(userInfo.getSysUser().getDept().getOrgId());
            dataWgWgsq.setManagerId(userInfo.getSysUser().getUserName());
            dataWgWgsqSq.setOrgId(userInfo.getSysUser().getDept().getOrgId());
            dataWgWgsqSq.setManagerId(userInfo.getSysUser().getUserName());
        }

        /**
         * 如果是管理员或者总行机关部室领导，则查询所有小区概况
         * 如果是支行行长或者客户经理，则查询所管户的小区列表
         */
        if(!userInfo.getRoles().contains("zhhz")
                && !userInfo.getRoles().contains("common")){
            /**
             * 如果sssq不为空，表示总行或者管理员查询某一个小区信息
             * 否则查询的为小区概况列表
             */
            if(StringUtils.isNotEmpty(dataWgWgsq.getSssq())){
                startPage();
                List<DataWgWgsq> list = dataWgWgsqService.queryGridSqAll(dataWgWgsq,userInfo);
                map.put("doList",getDataTable(list));
            } else {

                startPage();
                List<DataWgWgsqSq> list = dataWgWgsqSqService.selectDataWgWgsqSqList(dataWgWgsqSq);
                map.put("doList",getDataTable(list));
            }
        } else {
            /**
             * 查询所有小区列表
             */
            if(StringUtils.isNotEmpty(dataWgWgsq.getSssq())){
                startPage();
                List<DataWgWgsq> list = dataWgWgsqService.queryGridSqAll(dataWgWgsq,userInfo);
                map.put("doList",getDataTable(list));
            } else {
                startPage();
                List<DataWgWgsqSq> list = dataWgWgsqSqService.selectDataWgWgsqSqList(dataWgWgsqSq);
                map.put("doList",getDataTable(list));
            }

        }

        return AjaxResult.success(map);
    }

    
    
    /**
     * 查询包片社区居民列表
     */
    @ApiOperation(value="查询包片社区居民列表",notes="注意问题点")
    @PreAuthorize(hasPermi = "buss:gridSq:queryGridSqJm")
    @GetMapping("/queryGridSqJm")
    public TableDataInfo list(DataWgWgsq dataWgWgsq)
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgsq> list = dataWgWgsqService.selectDataWgWgsqList(dataWgWgsq,userInfo);
        return getDataTable(list);
    }
    
    
    
    @ApiOperation(value="删除网格社区信息")
    @PreAuthorize(hasPermi = "buss:gridSq:delWgsq")
    @GetMapping("/delWgsq")
    public AjaxResult delWgsq(DataWgWgsq dataWgWgsq) {
    	if(StringUtils.isEmpty(dataWgWgsq.getId())){
    		return AjaxResult.success();
    	}
		return dataWgWgsqService.deleteWgsq(dataWgWgsq);
    }
    
    
    @ApiOperation(value="查询小区列表")
    @PreAuthorize(hasPermi = "buss:gridSq:selectSssqList")
    @GetMapping("/selectSssqList")
    public AjaxResult selectSssqList() {
    
		return AjaxResult.success(dataWgWgsqService.selectSssqList());
    }
    
    
    /**
	 * 网格社区app提交网格营销任务
	 */
    @ApiOperation(value="网格社区app提交网格营销任务")
    @PreAuthorize(hasPermi = "buss:gridSq:sqTask")
	@RequestMapping(value = "/sqTask")
	public AjaxResult cjTask(@RequestBody DataWgWgsq vo)
	{
		if (StringUtils.isEmpty(vo.getPlanName())) {
			return AjaxResult.error(ErrorCode.IllegalArument);
		}
		LoginUser userInfo = tokenService.getLoginUser();
		AjaxResult error = dataWgWgsqService.sqTask(vo, userInfo);
		return error;
	}
    

    @ApiOperation(value="APP网格年概况折线图")
    @PreAuthorize(hasPermi = "buss:gridSq:wgsqAppBiYear")
    @GetMapping("/wgsqAppBiYear")
    public TableDataInfo wgsqAppBiYear()
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgsqSq> list = dataWgWgsqService.wgsqAppBiYear(userInfo);
        return getDataTable(list);
    }
    
    @ApiOperation(value="APP网格季度概况折线图")
    @PreAuthorize(hasPermi = "buss:gridSq:wgsqAppBiJd")
    @GetMapping("/wgsqAppBiJd")
    public TableDataInfo wgsqAppBiJd()
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgsqSq> list = dataWgWgsqService.wgsqAppBiYear(userInfo);
        return getDataTable(list);
    }
    
    @ApiOperation(value="APP网格月度概况折线图")
    @PreAuthorize(hasPermi = "buss:gridSq:wgsqAppBiMonth")
    @GetMapping("/wgsqAppBiMonth")
    public TableDataInfo wgsqAppBiMonth()
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgsqSq> list = dataWgWgsqService.wgsqAppBiMonth(userInfo);
        return getDataTable(list);
    }
    

    @ApiOperation(value="获取社区的小区或者社区,type  1社区 2小区")
    @PreAuthorize(hasPermi = "buss:gridSq:selectSqOrXq")
	@RequestMapping(value = "/selectSqOrXq")
	public TableDataInfo selectSqOrXq(@RequestBody DataWgWgsqSqgl vo)
	{
		if (vo == null || vo.getType() == null) {
			throw new CustomException(String.valueOf(ErrorCode.IllegalArument));
		}
		LoginUser userInfo = tokenService.getLoginUser();

        startPage();
		List<DataWgWgsqSqgl> list = dataWgWgsqService.selectSqOrXq(vo,userInfo);
		return getDataTable(list);
	}
    
    @ApiOperation(value="获取客户的基础信息")
    @PreAuthorize(hasPermi = "buss:gridSq:getKhInfo")
	@RequestMapping(value = "/getKhInfo")
	public AjaxResult getKhInfo(@RequestBody DataWgWgsqSqgl vo)
	{
		LoginUser userInfo = tokenService.getLoginUser();
        Map<String,Object> map = dataWgWgsqService.getKhInfo(vo,userInfo);
		return AjaxResult.success(map);
	}
    
    /**
     * 新增网格社区用户
     */
    @ApiOperation(value="新增客户-停用",notes="停用")
    @PreAuthorize(hasPermi = "buss:gridSq:add")
    @Log(title = "新增客户", businessType = BusinessType.INSERT)
    @PostMapping("/addWgsq")
    public AjaxResult add(@Validated  @RequestBody DataWgWgsq dataWgWgsq)
    {
        if(!ListUtil.isNullOrEmpty(dataWgWgsq.getDataGtgsh())){
            DataGtgsh dataGtgsh = dataWgWgsq.getDataGtgsh().get(0);
            if(StringUtils.isEmpty(dataGtgsh.getShmt())){
                throw  new CustomException("商户门头不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getSshy())){
                throw  new CustomException("所属行业不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getJyzk())){
                throw  new CustomException("经营状况不能为空");
            }
        }
        LoginUser userInfo = tokenService.getLoginUser();
        return toAjax(dataWgWgsqService.insertDataWgWgsq(dataWgWgsq,userInfo));
    }

    /**
     * 修改网格社区
     */
    @ApiOperation(value="修改社区客户-停用",notes="修改客户信息时必须传ID参数")
    @PreAuthorize(hasPermi = "buss:gridSq:edit")
    @Log(title = "修改社区客户", businessType = BusinessType.UPDATE)
    @PutMapping("/editWgsq")
    public AjaxResult edit(@Validated @RequestBody DataWgWgsq dataWgWgsq)
    {
        if (StringUtils.isEmpty(dataWgWgsq.getId())){
            throw  new CustomException("修改客户信息时ID不能为空");
        }
        if(!ListUtil.isNullOrEmpty(dataWgWgsq.getDataGtgsh())){
            DataGtgsh dataGtgsh = dataWgWgsq.getDataGtgsh().get(0);
            if(StringUtils.isEmpty(dataGtgsh.getZjhm())){
                throw  new CustomException("商户证件不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getShmt())){
                throw  new CustomException("商户门头不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getShzcmc())){
                throw  new CustomException("商户注册名称不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getJyfw())){
                throw  new CustomException("经营范围不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getFddbr())){
                throw  new CustomException("法人名称不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getFddbrzjhm())){
                throw  new CustomException("法人证件不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getSjjyr())){
                throw  new CustomException("实际经营人名称不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getSjjyrzjhm())){
                throw  new CustomException("实际经营人证件不能为空");
            }
            if(StringUtils.isEmpty(dataGtgsh.getJyzk())){
                throw  new CustomException("经营状况不能为空");
            }
        }

        LoginUser userInfo = tokenService.getLoginUser();
        return toAjax(dataWgWgsqService.updateDataWgWgsq(dataWgWgsq,userInfo));
    }

    /**
     * 导出网格社区列表
     */
    @ApiOperation(value="导出社区客户-停用",notes="")
    @PreAuthorize(hasPermi = "buss:gridSq:export")
    @Log(title = "网格社区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataWgWgsq dataWgWgsq) throws IOException
    {
        LoginUser userInfo = tokenService.getLoginUser();
        List<DataWgWgsq> list = dataWgWgsqService.selectDataWgWgsqList(dataWgWgsq,userInfo);
        ExcelUtil<DataWgWgsq> util = new ExcelUtil<DataWgWgsq>(DataWgWgsq.class);
        util.exportExcel(response, list, "网格社区数据");
    }

    /**
     * 删除网格社区
     */
    @ApiOperation(value="删除社区客户-停用",notes="")
    @PreAuthorize(hasPermi = "buss:gridSq:remove")
    @Log(title = "网格社区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dataWgWgsqService.deleteDataWgWgsqByIds(ids));
    }

}
